﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Zone
namespace backoffice.Data
{
    partial class Zone
    {
        public void setZone(Zone z)
        {
            this.ZoneID = z.ZoneID;
            this.CountryID = z.CountryID; 
            this.Name = z.Name;
            this.Areas = z.Areas;
            this.AgriculturePractices = z.AgriculturePractices;
        }
    }
}
#endregion

#region dataZone
namespace backoffice.Data
{
    public class dataZone
    {
        #region Create
        public static void createZone(Zone z)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                z.ZoneID = Guid.NewGuid();

                db.Zones.InsertOnSubmit(z);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void createZone_Plant(Zones_Plant zp)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                zp.ZonePlantID = Guid.NewGuid();

                db.Zones_Plants.InsertOnSubmit(zp);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Zone getZone(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Zones.FirstOrDefault(z => z.ZoneID == guid);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Zone> getZones(Expression<Func<Zone, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Zones.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Zones_Plant> getZone_Plant(Expression<Func<Zones_Plant, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Zones_Plants.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeZone(Zone z)
        {
            try
            {
                if (z == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Zone res = db.Zones.FirstOrDefault(zone => zone.ZoneID == z.ZoneID);
                    if (res == null) return false;
                    else
                    {
                        res.setZone(z);
                        db.SubmitChanges();
                        return true;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteZone(Zone z)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Zones.Contains(z))
                {
                    Zone res = db.Zones.FirstOrDefault(zone => zone.ZoneID == z.ZoneID);

                    if (res == null) return false;
                    else
                    {
                        // Delete zone_plants
                        IQueryable<Zones_Plant> zp = db.Zones_Plants.Where((Zones_Plant z_p) => z_p.ZoneID == res.ZoneID);
                        db.Zones_Plants.DeleteAllOnSubmit(zp);

                        // Delete coordinates
                        IQueryable<Coordinate> c = db.Coordinates.Where((Coordinate co) => co.ZoneID == res.ZoneID);
                        db.Coordinates.DeleteAllOnSubmit(c);

                        // Update land
                        IQueryable<Land> lands = db.Lands.Where((Land la) => la.ZoneID == res.ZoneID);

                        foreach (Land l in lands)
                        {
                            l.ZoneID = null;
                        }

                        // Delete zone
                        db.Zones.DeleteOnSubmit(res);
                        db.SubmitChanges();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion